#pragma once
#include "ThreadPool.h"
#include "TcpServer.h"
#include "Configuration.h"
#include "TcpConnection.h"
#include "ProtocolParser.h"
#include "KeyRecommander.h"
#include "WebPageSearcher.h"
#include "Message.h"
#include <memory>
#include <functional>

using TcpConnectionPtr = std::shared_ptr<TcpConnection>;

class SearchEngineServer
{
public:
    static SearchEngineServer *getInstance();

    void start();
    void stop();

    // Reactor回调函数（IO线程）
    void onConnection(TcpConnectionPtr conn);
    void onMessage(TcpConnectionPtr conn, const std::string &msg);
    void onClose(TcpConnectionPtr conn);

    // 计算线程处理函数
    void doTaskThread(TcpConnectionPtr conn, std::string &msg);

private:
    SearchEngineServer();
    void init();
    void loadConfiguration();

    static SearchEngineServer *_instance;

    // 严格按PDF的成员变量
    std::unique_ptr<ThreadPool> _threadpool; // 线程池
    std::unique_ptr<TcpServer> _tcpserver;   // TCP服务器
    std::unique_ptr<Configuration> _config;  // 配置

    // 严格按PDF的业务模块
    ProtocolParser _protocol_parser;
    KeyRecommander _key_recommander;
    WebPageSearcher _webpage_searcher;

    // 服务器配置
    int _server_port;
    int _thread_pool_size;
};